﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CarbonExchange.Bll;

namespace CarbonExchange.WebControls
{
    [ToolboxData("<{0}:AuditQuestionContainer runat=server></{0}:AuditQuestionContainer>")]
    public class AuditQuestionContainer : CompositeControl
    {
        private List<string> UserControlPaths;
        public int SectionId { get; set; }
        public int SiteId { get; set; }
        public Audit Audit { get; set; }

        protected override object SaveViewState()
        {
            ViewState["UserControlPaths"] = UserControlPaths;
            ViewState["SectionId"] = SectionId;
            ViewState["SiteId"] = SiteId;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            UserControlPaths = (List<string>)ViewState["UserControlPaths"];
            SectionId = (int)ViewState["SectionId"];
            SiteId = (int)ViewState["SiteId"];
        }

        protected override void Render(HtmlTextWriter writer)
        {
            var questions = this.Controls.OfType<CarbonExchange.WebControls.AuditQuestionBase>().ToArray();
            int questionItems = questions.Select(x => x.GetActiveItemsCount()).Sum();

            int itemsPerCol = questionItems / 3;
            int rem = questionItems % 3;
            int currentQuestionIndex = 0;
            int partialRendered = 0;
            for (int col = 0; col < 3; col++)
            {
                int rendered = 0;
                int start = (itemsPerCol * col) + (rem > col ? col : rem);
                int end = start + itemsPerCol +(rem > col ? 1 : 0);
                writer.AddAttribute(HtmlTextWriterAttribute.Class, string.Format("col {0}", col == 0 ? "first" : col == 2 ? "last" : "").Trim());
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                while (rendered < end - start && questions.Count() > currentQuestionIndex)
                {
                    int renderedInThisQuestion = questions[currentQuestionIndex].RenderItems(partialRendered, end - rendered, writer);
                    rendered += renderedInThisQuestion;
                    if (rendered < end - start || partialRendered + renderedInThisQuestion == questions[currentQuestionIndex].GetActiveItemsCount())
                    {
                        currentQuestionIndex++;
                        partialRendered = 0;
                    }
                    else if (rendered == end - start)
                    {
                        partialRendered += renderedInThisQuestion;
                    }
                }
                writer.RenderEndTag();//HtmlTextWriterTag.Div
            }
        }

        protected override void CreateChildControls()
        {
            if (this.Controls.Count == 0 && UserControlPaths != null)
            {
                var userControls = UserControlPaths.Select(x => Page.LoadControl(x) as CarbonExchange.WebControls.AuditQuestionBase);
                foreach (var item in userControls)
                {
                    this.Controls.Add(item);
                }
            }
            base.CreateChildControls();
        }

        public override void DataBind()
        {
            base.DataBind();
            SiteQuestionFactory factory = new SiteQuestionFactory();
            var questions = factory.GetAllBySectionId(SectionId, SiteId).ToList();
            var configs = questions.Select(x => new QuestionTextConfiguration(x));
            UserControlPaths = configs.Select(x => x.UserControlPath).ToList();
            var userControls = UserControlPaths.Select(x => Page.LoadControl(x) as AuditQuestionBase);
            var i = 0;
            foreach (var item in userControls)
            {
                this.Controls.Add(item);
                item.SetContext(configs.ElementAt(i), SiteId, Audit);
                item.DataBind();
                i++;
            }
        }

    }
}
